#include "ustc_comdef.h"
#include "ustc_hif.h"
#include "ustc_api.h"

#define USTC_TP_SAMPLE_CNT	10
#define USTC_TP_Z_MAX		300

typedef struct _USTC_TP_T_
{
	unsigned short coff[5][3];
	unsigned short coff_idx;
	unsigned short coordinate[5][2];
	unsigned short sample[USTC_TP_SAMPLE_CNT][3];
	unsigned short point[3];
	unsigned short delta_x;
	unsigned short delta_y;
	unsigned short w;
	unsigned short h;
	unsigned short pressed;
	unsigned short idx;
	unsigned short cnt;
}USTC_TP_T;

volatile USTC_TP_T ustc_pen;

USTC_U16 (*disp_buf)[240]=(USTC_U16 (*)[240])0xC018000;

extern USTC_U32 VectorTable[];
extern void lcd_init(void);

void __irq isr_ustc_tp()
{
	ustc_tp_int_disable();

	ustc_pen.pressed=1;
	
#if (S3C44B0_TEST_PLATFORM>0)
	*(unsigned long*)0x1D20054=1;//clear EINT4 pending
	*(unsigned long*)0x1E00024=0x200000;//clear EINT4 pendding
#endif
}

void image_init()
{
	int i,j;

	for (i=0; i<320; i++)
		for (j=0; j<240; j++)
			disp_buf[i][j]=0xF800;
}

void image_success()
{
	int i,j;

	for (i=0; i<320; i++)
		for (j=0; j<240; j++)
			disp_buf[i][j]=0x7E0;
		
	dl_lcd_ILI9320_16BIT_display_scn(0, 0, 240, 320, (unsigned char*)disp_buf);
}

void image_star(unsigned short x, unsigned short y)
{
	int xx=0,yy=0;
	int xe=240,ye=320;
	int i,j;

	if (x>7)
		xx=x-7;

	if (y>7)
		yy=y-7;

	if (x+7<240)
		xe=x+7;

	if (y+7<320)
		ye=y+7;

	for (j=-1; j<2; j++)
	for (i=xx; i<=xe; i++)
		disp_buf[y+j][i]=0x1F;
	
	for (j=-1; j<2; j++)
	for (i=yy; i<=ye; i++)
		disp_buf[i][x+j]=0x1F;

	dl_lcd_ILI9320_16BIT_display_scn(0, 0, 240, 320, (unsigned char*)disp_buf);
}

void ustc_touch_init()
{
	ustc_pen.idx=0;
	ustc_pen.cnt=0;
	ustc_pen.pressed=0;
	ustc_pen.coff_idx=0;

	ustc_pen.coordinate[0][0]=10;
	ustc_pen.coordinate[1][0]=10;
	ustc_pen.coordinate[2][0]=230;
	ustc_pen.coordinate[3][0]=230;
	ustc_pen.coordinate[4][0]=120;

	ustc_pen.coordinate[0][1]=10;
	ustc_pen.coordinate[1][1]=310;
	ustc_pen.coordinate[2][1]=310;
	ustc_pen.coordinate[3][1]=10;
	ustc_pen.coordinate[4][1]=160;

	ustc_pen.w=40;
	ustc_pen.h=60;
}

int ustc_tp_diff_point(USTC_U16 x, USTC_U16 y, USTC_U16 z)
{
	if (!ustc_pen.cnt)
		return 1;

	if (ustc_pen.sample[ustc_pen.idx][0]-ustc_pen.delta_x>x ||
		ustc_pen.sample[ustc_pen.idx][0]+ustc_pen.delta_x<x)
		return 1;

	if (ustc_pen.sample[ustc_pen.idx][1]-ustc_pen.delta_y>y ||
		ustc_pen.sample[ustc_pen.idx][1]+ustc_pen.delta_y<y)
		return 1;

	return 0;
}

void ustc_touch_center()
{
	int i;

	ustc_pen.coff[4][0]=0;
	ustc_pen.coff[4][1]=0;
	
	for (i=0; i<4; i++)
	{
		ustc_pen.coff[4][0]+=ustc_pen.coff[i][0];
		ustc_pen.coff[4][1]+=ustc_pen.coff[i][1];
	}

	ustc_pen.coff[4][0]/=4;
	ustc_pen.coff[4][1]/=4;

	ustc_pen.delta_x=(ustc_pen.coff[3][0]+ustc_pen.coff[2][0]-ustc_pen.coff[1][0]-ustc_pen.coff[0][0])/ustc_pen.w;
	ustc_pen.delta_y=(ustc_pen.coff[1][1]+ustc_pen.coff[2][1]-ustc_pen.coff[3][1]-ustc_pen.coff[0][1])/ustc_pen.h;
}

void test_tp()
{
	int i;
	
	USTC_U16 x,y,z;
	
	ustc_tp_init();

	lcd_init();

	dl_lcd_ILI9320_16BIT_display_scn(0, 0, 240, 320, (unsigned char*)disp_buf);

	VectorTable[13]=(USTC_U32)isr_ustc_tp;

	enable_irq(0);

	ustc_touch_init();
	
	while (1)
	{

		ustc_pen.pressed=0;
		ustc_pen.cnt=0;
		ustc_tp_int_enable();
		image_init();
		image_star(ustc_pen.coordinate[ustc_pen.coff_idx][0], ustc_pen.coordinate[ustc_pen.coff_idx][1]);

		while (!ustc_pen.pressed)
			;

		do
		{
			ustc_delay(10000);
			ustc_tp_position(&x, &y, &z);

			if (z>USTC_TP_Z_MAX)
			{
				break;
			}

			if (ustc_pen.idx>=USTC_TP_SAMPLE_CNT)
				ustc_pen.idx =0;
			
			ustc_pen.sample[ustc_pen.idx][0]=x;
			ustc_pen.sample[ustc_pen.idx][1]=y;
			ustc_pen.sample[ustc_pen.idx][2]=z;

			ustc_pen.idx++;
			ustc_pen.cnt++;

		}while (1);

		if (!ustc_pen.cnt)
			continue;
		
		if (ustc_pen.coff_idx<4)
		{
			ustc_pen.coff[ustc_pen.coff_idx][0]=ustc_pen.sample[ustc_pen.idx-1][0];
			ustc_pen.coff[ustc_pen.coff_idx][1]=ustc_pen.sample[ustc_pen.idx-1][1];
			ustc_pen.coff[ustc_pen.coff_idx][2]=ustc_pen.sample[ustc_pen.idx-1][2];
			ustc_pen.coff_idx++;

			if (ustc_pen.coff_idx>3)
				ustc_touch_center();//calculate center point
		}else{
			if (!ustc_tp_diff_point(ustc_pen.coff[4][0], ustc_pen.coff[4][0], z))
			{//press to continue
				image_success();
				ustc_pen.pressed=0;
				ustc_pen.cnt=0;
				ustc_tp_int_enable();

				while (!ustc_pen.pressed)
					;
			}

			ustc_pen.coff_idx=0;
		}
		
	}
}

int c_main()
{

	test_tp();

	return 0;
}
